//
//  ipv4_defragmentor.h
//  testpcap
//
//  Created by bugforever on 1/15/15.
//  Copyright (c) 2015 bugforever. All rights reserved.
//

#ifndef __ipv4_defragmentor_h
#define __ipv4_defragmentor_h

#include "../../Include/network_struct.h"
#include "../protocol_parser/ipv4_parser.h"

#include <list>
#include <map>

struct ipv4_fragment
{
    u_char saddr[4];
    u_char daddr[4];
    
    bool is_last_fragment;
    int fragment_offset;
    
    interface_network_data * ptr_data;
};

struct ipv4_fragment_list
{
    unsigned long latest_fragment_time;
    std::list< ipv4_fragment * > * ptr_list;
};


class ipv4_defragmentor
{
public:
    ipv4_defragmentor();
    virtual ~ipv4_defragmentor();
    
    virtual int defragment( ipv4_data * ptr_input , network_ipv4_data * ptr_output );
    
protected:
    virtual void copy_ipv4data( ipv4_data * ptr_input , network_ipv4_data * ptr_output );
    virtual ipv4_fragment * create_fragment( ipv4_data * ptr_data );
    virtual ipv4_fragment_list * add_fragment( ipv4_data * ptr_data );
    virtual bool is_fragmentlist_complete( ipv4_fragment_list * ptr_list );
    virtual void clean_timeout_fragmentlist();
    virtual void destroy_fragmentlist( ipv4_fragment_list * ptr_list );
    
    virtual void defragment_fragmentlist( ipv4_fragment_list * ptr_list , network_ipv4_data * ptr_output );
    
    virtual void destroy_fragments_map();
    
protected:
    std::map< int , ipv4_fragment_list * > m_fragments_map;
    
};


#endif /* defined(__testpcap__ipv4_defragmentor__) */
